using System;
using System.Collections;
using System.Data;
using PCSComProduct.Items.DS;
using PCSComProduction.WorkOrder.DS;
using PCSComUtils.Common;

using PCSComUtils.MasterSetup.DS;
using PCSComUtils.PCSExc;

namespace PCSComProduction.WorkOrder.BO
{
	public class WOBOMBO
	{
        public void UpdateDataSet(DataSet dstData)
        {
            ITM_BOMDS dsBOM = new ITM_BOMDS();
            dsBOM.UpdateDataSet(dstData);
        }

		public DataRow GetInfoForWOBom(int pintID)
		{
			return new ITM_ProductDS().GetInfoForWOBom(pintID);
		}        
		public object GetObjectUM(int pintUMID)
		{
			try
			{
				return new MST_UnitOfMeasureDS().GetObjectVO(pintUMID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public DataTable ListRoutingOfProduct(int pintProductID)
		{
			try
			{
				return new ITM_RoutingDS().ListRoutingByProduct(pintProductID).Tables[0];
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public object GetObjectVOForBOMByCode(string pstrCode)
		{
			try
			{
				return new ITM_ProductDS().GetObjectVOForBOMByCode(pstrCode);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public void UpdateAll(DataSet dstData)
		{
			//update dataset BOM
			//ITM_BOMDS dsBOM = new ITM_BOMDS();
			PRO_WOBOMDS dsWOBOM = new PRO_WOBOMDS();
			DataSet dstBOM = new DataSet();
			//dstBOM.Tables.Add(dstData.Tables[ITM_BOMTable.TABLE_NAME].Copy());
			dstBOM.Tables.Add(dstData.Tables[PRO_WOBOMTable.TABLE_NAME].Copy());
			//dsBOM.UpdateDataSet(dstBOM);
			dsWOBOM.UpdateDataSet(dstBOM);

			dstData.AcceptChanges();
		}

		public DataTable ListBOMDetailsOfProduct(int pintProductID, int pintWorkorderdetailID)
		{
			PRO_WOBOMDS dsBOM = new PRO_WOBOMDS();
			return dsBOM.ListBomDetailOfProduct(pintProductID,pintWorkorderdetailID);
		}
		public DataSet ListBOMDetailsOfProduct(int pintProductID, int pintWorkorderdetailID, int pintProductTypeID)
		{
			PRO_WOBOMDS dsBOM = new PRO_WOBOMDS();
			return dsBOM.ListBomDetailOfProduct(pintProductID,pintWorkorderdetailID,pintProductTypeID);
		}

		public DataTable ListHierarchyOfProduct(int pintProductID)
		{
			try
			{
				return new ITM_HierarchyDS().ListForProduct(pintProductID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public ArrayList CheckBussinessForBOM(DataTable pdtbComponent, int pintProductID)
		{
			try
			{
				ArrayList arrReturn = new ArrayList();
				ITM_HierarchyDS dsHierarchy = new ITM_HierarchyDS();
				DataTable dtbParent = dsHierarchy.ListParentOfProduct(pintProductID);
				dsHierarchy = null;

				//compare to find a wrong component
				for (int i =0; i <pdtbComponent.Rows.Count; i++)
				{
					if (pdtbComponent.Rows[i].RowState != DataRowState.Deleted)
					{
						foreach (DataRow drow in dtbParent.Rows)
						{
							if (pdtbComponent.Rows[i][ITM_BOMTable.COMPONENTID_FLD].ToString().Trim() == drow[0].ToString().Trim())
							{
								arrReturn.Add(pdtbComponent.Rows[i][ITM_BOMTable.COMPONENTID_FLD].ToString().Trim() + ";" + i.ToString());
							}
						}
					}
				}
				return arrReturn;
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.Message, ex);
			}
		}

		public string ConditionToSearchItem(int pintCCNID, EnumAction enumAction)
		{
			string strCondition = " WHERE " + v_ITMBOM_Product.VIEW_NAME + "." + ITM_ProductTable.CCNID_FLD + " = " + pintCCNID.ToString() + " and " + v_ITMBOM_Product.VIEW_NAME + "." + ITM_ProductTable.MAKEITEM_FLD + " = 1 and "
				+ v_ITMBOM_Product.VIEW_NAME + "." + v_ITMBOM_Product.HASBOM_FLD + " = ";
			if (enumAction == EnumAction.Add)
			{
				strCondition += "0";
			}
			else
			{
				strCondition += "1";
			}
			return strCondition;
		}

		public DataSet ListComponents()
		{
			ITM_BOMDS dsBOM = new ITM_BOMDS();
			return dsBOM.ListComponents();
		}
		public DataSet ListComponents(int pintProductID)
		{
			ITM_BOMDS dsBOM = new ITM_BOMDS();
			return dsBOM.ListComponents(pintProductID);
		}

		public DataRow GetItemInfo(int pintProductID)
		{
			ITM_ProductDS dsProduct = new ITM_ProductDS();
			return dsProduct.GetItemInfo(pintProductID);
		}

		public string GetWorkCenterInfo(int workCenterId)
		{
			MST_WorkCenterDS objMST_WorkCenterDS  = new MST_WorkCenterDS ();
			MST_WorkCenterVO voWorkCenter = (MST_WorkCenterVO) objMST_WorkCenterDS.GetObjectVO(workCenterId);					
			if(voWorkCenter != null)
			{
				return voWorkCenter.Code;
			}

			return string.Empty;
		}
		public bool IsEditable(int pintWorkOrderDetailID, int pintComponentID)
		{
			PRO_WOBOMDS dsBOM = new PRO_WOBOMDS();
			return dsBOM.IsEditable(pintWorkOrderDetailID, pintComponentID);
		}
	}
}
